const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    lunbo1: '',
    lunbo2: '',
    lunbo3: '',
    is_like: false,
    tab_key: "pro_des", // 详情：pro_des，属性：pro_nature
    num: 1,
    // 使用data数据对象设置样式名  
    minusStatus: 'disabled',
    is_show_modal: false,
    productClassify: '',
    productName: '',
    discount: "",
    productPrice: "",
    productWeight: "",
    pro_id: "",
    agency:'',
    levle:'',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    let id = options.id
    let levle = options.levle
    var that = this

    wx.getStorage({
      key: 'openid',
      success: function(res) {
        let openid = res.data
        wx.showLoading({
          title: '加载中',
        })
        wx.request({
          url: app.globalData.URL + '/app/memberCard/cardDetails?id=' + id + '&openid=' + openid + '&levle=' + levle,
          success: function (res) {
            console.log(res.data)
            wx.hideLoading()
            that.setData({
              pro_id: res.data.emberCard.id,
              productName: res.data.emberCard.cardName,
              discount: res.data.emberCard.price,
              productPrice: res.data.emberCard.cardMoney,
              productWeight: res.data.emberCard.levleNotes,
              levle: res.data.emberCard.levle,
              agency:res.data.emberCard.agency
            })
          }
        })
      },
    })

  },
  skipShoppingCart: function () {
    wx.navigateTo({
      url: '../shoppingCart/shoppingCart'
    })
  },
  skipMy: function () {
    wx.switchTab({
      url: '../my/my'
    })
  },
  showModal: function () {
    this.setData({
      is_show_modal: true
    })
  },
  closeModal: function () {
    this.setData({
      is_show_modal: false
    })
  },
  changeDes: function () {
    this.setData({
      tab_key: "pro_des"
    })
  },
  changeNature: function () {
    this.setData({
      tab_key: "pro_nature"
    })
  },

  bindMinus: function () {
    var num = this.data.num;
    // 如果大于1时，才可以减  
    if (num > 1) {
      num--;
    }
    // 只有大于一件的时候，才能normal状态，否则disable状态  
    var minusStatus = num <= 1 ? 'disabled' : 'normal';
    // 将数值与状态写回  
    this.setData({
      num: num,
      minusStatus: minusStatus
    });
  },
  /* 点击加号 */
  bindPlus: function () {
    var num = this.data.num;
    // 不作过多考虑自增1  
    num++;
    // 只有大于一件的时候，才能normal状态，否则disable状态  
    var minusStatus = num < 1 ? 'disabled' : 'normal';
    // 将数值与状态写回  
    this.setData({
      num: num,
      minusStatus: minusStatus
    });
  },
  /* 输入框事件 */
  bindManual: function (e) {
    var num = e.detail.value;
    // 将数值与状态写回  
    this.setData({
      num: num
    });
  },

  //立即购买跳转购买清单
  toPay: function () {
    var levleNum = '';
    
    if (this.data.agency != null) {
      console.log('agency', this.data.agency)
      if (this.data.agency == 0) {
        if (this.data.levle == 'A') {
          levleNum = 180;
        } else if (this.data.levle == 'B') {
          levleNum = 60;
        } else if (this.data.levle == 'C') {
          levleNum = 15;
        } else {
          levleNum = 2;
        }
      }
    }
    var that = this
    
    if(this.data.num < levleNum){
      
      wx.getStorage({
        key: 'openid',
        success: function(res) {
          let openid = res.data
          wx.request({
            url: app.globalData.URL + '/app/memberCard/cardNum?openid='+ openid + '&number=' + that.data.num,
            success:function(res){
              console.log(res)
              if(res.data.flag == true){
                wx.showModal({
                  title: '提示',
                  content: '首单数量不小于' + levleNum,
                })
              }else{
                wx.showModal({
                  title: '提示1',
                  content: res.data.msg,
                })
              }
            }
          })
        },
      })
    }else{
      
      wx.getStorage({
        key: 'openid',
        success: function(res) {
          let openid = res.data
          
          wx.request({
            url: app.globalData.URL + '/app/memberCard/settlement?openid=' + openid + '&id=' + that.data.pro_id + '&number=' + that.data.num + '&levle=' + that.data.levle + '&delId=',
            success: function (res) {
              console.log(res)
              var name = res.data.delivery.name || ''
              var phone = res.data.delivery.telPhone || ''
              var address = res.data.delivery.address || ''
              var dlid = res.data.delivery.id || ''
              var number = res.data.emberCard.number
              var price = res.data.emberCard.price

              wx.navigateTo({
                url: '../mShoppingList/mShoppingList?name=' + name + '&phone=' + phone + '&address=' + address + '&number=' + number + '&price=' + price + '&levle=' + that.data.levle + '&id=' + that.data.pro_id + '&dlid=' + dlid,
              })
            }
          })
        },
      })
    }
    
  }
})